-
Notifications
You must be signed in to change notification settings - Fork 51
pre-commit hook for formatting JSON files #1256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
tests/data/power_flow/asym-line/rmatrix-xmatrix-c1-c0/input.json
Outdated
Show resolved
Hide resolved
|
Hi @kornerc, Thank you for the contribution. We actually have been thinking about doing this in the past. For some configuration files (e.g. However, for the data files (
This exact reason is why we deliberately use a compact notation with a maximum indentation level (see this formatter and its configuration). That said, having consistent formatting helps with both code quality and code reviews. Here are a couple potential paths going forward:
One last remark: to keep the git commit history clean, can you please either squash the commits after you're done reverting (at the latest when sending it to the merge queue) or open a new PR? |
|
Hi @kornerc, thanks for proposing this new improvement. In addition of what @mgovers said, I would say ignore all PGM data json files. These files already have a custom indent pattern which is produced by the serializer. Also, user/contributor might want to a specific custom indent for a certain data file for some reason (usually modelling or readability). Enforcing a rule on PGM data json files does not make sense. So I would go for option 3 of @mgovers proposed:
|
Signed-off-by: Clemens Korner <[email protected]>
edff95a to
7652dce
Compare
Signed-off-by: Clemens Korner <[email protected]>
|
Hi @mgovers and @TonyXiang8787 thank you for your review! Based on your feedback I made some changes:
I am happy to hear your thoughts! |
mgovers
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job, very nice reformatting. I agree with your choice and the changes. Thank you for taking the time for researching the various possibilities.
I didn't manage to set up the VS Code extension in the devcontainer properly and instead adjusted the default formatter in .vscode/settings.json to function similar to Biome. I can configure the devcontainer for Biome properly in another PR.
i think that that is alright, but with the existing configuration, i do get suggestions along the lines of
{
- "calculation_method": ["linear", "newton_raphson", "iterative_current", "linear_current"],
+ "calculation_method": [ "linear", "newton_raphson", "iterative_current", "linear_current" ],
"rtol": 1e-5,
"atol": 1e-5
}If you want to postpone recommendation for Biome, then please try to see whether you can turn off those extra whitespace suggestions, or otherwise whether you can configure either Biome to add them. If neither of the options are feasible, it's fine to keep it as-is for now.
I've noticed in the last run of the actions that Biome formats irrelevant files e.g. files in the .mypy_cache.
I can add these folders to the blacklisted files after the rest of the PR is approved so that I can still easily revert the commit which formats all the files.
good choice. Please indeed blacklist them.
| // See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations. | ||
| // Extension identifier format: ${publisher}.${name}. Example: vscode.csharp | ||
| // List of extensions which should be recommended for users of this workspace. | ||
| "recommendations": [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add biome here as well
.vscode/settings.json
Outdated
| // can be removed when Biome is used in VS Code as formatter | ||
| "json.format.keepLines": true, | ||
| "[json][jsonc]": { | ||
| "editor.tabSize": 2, | ||
| "editor.insertSpaces": true, | ||
| "editor.detectIndentation": false | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you add biome to vscode workspace recommendations, then you can configure that as the default formatter
| ] | ||
| } | ||
| ] | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
surprised to see newline at EOF because i thought it's not officially part of the JSON standard (but it is for JSONC) but as long as the code generator + CMake etc. work fine, it's alright, and it seems that they do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked into RFC 8259 (The JavaScript Object Notation (JSON) Data Interchange Format) and section 2 explicitely allows insignificant whitespaces like new lines at the end:
A JSON text is a sequence of tokens. The set of tokens includes six
structural characters, strings, numbers, and three literal names.A JSON text is a serialized value. Note that certain previous
specifications of JSON constrained a JSON text to be an object or an
array. Implementations that generate only objects or arrays where a
JSON text is called for will be interoperable in the sense that all
implementations will accept these as conforming JSON texts.JSON-text = ws value wsThese are the six structural characters:
begin-array = ws %x5B ws ; [ left square bracket begin-object = ws %x7B ws ; { left curly bracket end-array = ws %x5D ws ; ] right square bracket end-object = ws %x7D ws ; } right curly bracket name-separator = ws %x3A ws ; : colon value-separator = ws %x2C ws ; , commaInsignificant whitespace is allowed before or after any of the six
structural characters.ws = *( %x20 / ; Space %x09 / ; Horizontal tab %x0A / ; Line feed or New line %x0D ) ; Carriage return
and accordingly to the POSIX standard a line must end with a new line:
A sequence of zero or more non- characters plus a terminating character.
So my own interpretation is: it does not hurt to have a newline at the EOF for a JSON file and it makes such a file a valid POSIX text file.
There exists is also a related ticket for VS Code
|
Hi @kornerc, We would prefer a formatter which can be installed from Your proposal of |
|
Hi @kornerc, After some digging. I think we can add So first create a {
"private": true,
"devDependencies": {
"biomejs/biome": "*",
"markdownlint-cli": "*"
}
}Add Then run Then we can change CI code quality, to install Then we modify the refresh dependency job to also refresh Finally change the build guide to instruct developers use |
Co-authored-by: Martijn Govers <[email protected]> Signed-off-by: Clemens Korner <[email protected]>
Signed-off-by: Clemens Korner <[email protected]>
Update: I managed to setup the VS-Code extension (Biome needs to be installed).
Biome now parses the
Yes I like the idea to also automatically update the node dependencies.
Update: The VS Code extension needs Biome to be installed I will have a look if it is able to use the Biome binary installed with precommit. |
|
Hi @kornerc, Thank you again for the extensive investigation and update. Let us know if there's anything you need. |
Signed-off-by: Clemens Korner <[email protected]>
Signed-off-by: Clemens Korner <[email protected]>
Signed-off-by: Clemens Korner <[email protected]>
Changes proposed in this PR include:
testsandcode_generationare formattedpre-commit run --all-files pretty-format-jsonhas been executed to re-format all JSON files.This has been done in a separate commit so that these changes can be easily reverted if formatting options need to be adjusted
I am not sure if this pre-commit hook is wanted.
Feel free to close this PR if this is behavior is not desired.
Could you please pay extra attention to the points below when reviewing the PR:
I hope that this does not cause any unforseen (numerical) issues.
E.g.
10.5e3->10500.01e-5->1e-05